package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.BeanUtils;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.LocationVoMapper;
import com.mt.roll.wms.params.LocationQueryParam;
import com.mt.roll.wms.params.LocationShelfProcessQueryParam;
import com.mt.roll.wms.service.LocationService;
import com.mt.roll.wms.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author weihongyang
 * @Date 2021/11/11 9:54 上午
 * @Version 1.0
 */
@Transactional
@Service
@Slf4j
public class LocationServiceImpl extends BaseService implements LocationService {

    @Autowired
    private LocationVoMapper locationVoMapper;

    @Autowired
    private LocationServiceBiz locationServiceBiz;

    @Autowired
    private ShelfServiceBiz shelfServiceBiz;

    @Autowired
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;

    @Autowired
    private LocationStorageSubServiceBiz locationStorageSubServiceBiz;

    @Autowired
    private StorageBoxServiceBiz storageBoxServiceBiz;

    @Override
    public LocationVo getLayersAndColumnsByStorageId(Long storageId) {
        return locationVoMapper.getLayersAndColumnsByStorageId(storageId);
    }

    @Override
    public R<List<LocationVo>> list(LocationQueryParam alarmQueryParam) {
        List<Location> locationList = locationServiceBiz.list();
        List<LocationVo> locationVoList = BeanUtils.copyList(locationList, LocationVo.class);
        return successful(locationVoList);
    }

    @Override
    public R<List<LocationProcessVo>> getAllLocationByProcessIdOrShelfId(LocationShelfProcessQueryParam locationShelfProcessQueryParam) {
        List<LocationProcessVo> allLocationProcessById = locationVoMapper.getAllLocationByProcessIdOrShelfId(locationShelfProcessQueryParam);
        return R.ok(allLocationProcessById);
    }

    @Override
    public R<List<LocationShelfListVO>> getShelfList(){

        List<ShelfVo> shelfVoList = new ArrayList<>();
        //先用着
        CassetteVo cassetteVo = CassetteVo.builder().build();
        cassetteVo.setId(1L);
        cassetteVo.setName("cassette");
        cassetteVo.setStatus(1L);
        List<CassetteVo> cassetteVos = new ArrayList<>();
        cassetteVos.add(cassetteVo);

        List<Shelf> shelfList = shelfServiceBiz.list();
        for(Shelf shelf:shelfList){
            ShelfVo shelfVo = ShelfVo.builder().build();
            shelfVo.setId(shelf.getId());
            shelfVo.setName(shelf.getShelfName());
            //获取对应库位
//            QueryWrapper<Location> wrapper = new QueryWrapper<>();
//            wrapper.eq(Location.SHELF_ID,shelf.getId());
//            List<Location> locationList = locationServiceBiz.list(wrapper);
            LocationShelfProcessQueryParam locationShelfProcessQueryParam = new LocationShelfProcessQueryParam();
            locationShelfProcessQueryParam.setShelfId(shelf.getId());
            List<LocationProcessVo> allLocationProcessById = locationVoMapper.getAllLocationByProcessIdOrShelfId(locationShelfProcessQueryParam);
            List<PortVo> portList = new ArrayList<>();
            for(LocationProcessVo l:allLocationProcessById){
                PortVo portVo = PortVo.builder().build();
//                portVo.setAttribute("1");
                portVo.setCassetteVoList(cassetteVos);
                portVo.setId(l.getId());
                if(l.getPortAttrId()==null){
                    portVo.setAttribute(null);
                }else {
                    portVo.setAttribute(l.getPortAttrId().toString());
                }

                portVo.setName(l.getLocationName());
                portVo.setLayers(Integer.parseInt(l.getLayers()));
                portVo.setColumns(Integer.parseInt(l.getColumns()));
                portList.add(portVo);
            }
            //在对port分组
            Map<Integer, List<PortVo>> baseManageMap =
                    portList.stream().collect(Collectors.groupingBy(PortVo::getLayers));
            //分组后扔进row
            List<RowVo> rowVoList = new ArrayList<>();
            for (Map.Entry<Integer, List<PortVo>> entry : baseManageMap.entrySet()){
                RowVo row = RowVo.builder().build();
                row.setId(entry.getKey().toString());
                row.setName("row" + entry.getKey());
                List<PortVo> tempPortList = entry.getValue();
                List<PortVo> tempPortList2 = tempPortList.stream().sorted(Comparator.comparing(PortVo::getColumns)).collect(Collectors.toList());
                row.setPortVoList(tempPortList2);
                rowVoList.add(row);
            }
            shelfVo.setRowVoList(rowVoList);
            shelfVoList.add(shelfVo);
        }
        List<LocationShelfListVO> locationShelfListVOList = new ArrayList<>();
        LocationShelfListVO locationShelfListVO = LocationShelfListVO.builder().build();
        locationShelfListVO.setShelfVoList(shelfVoList);
        locationShelfListVOList.add(locationShelfListVO);
        return successful(locationShelfListVOList);
    }

    @Override
    public R<SubListAndStorageFidVo> getSubListAndStorageFidByLocationNum(IdParam idParam){
        SubListAndStorageFidVo subListAndStorageFidVo = SubListAndStorageFidVo.builder().build();
        String[] substrateCodes;
        String storageBoxFid;
//        List<SubstrateVo> substrateVoList = new ArrayList<>();
        //先获取得到tsbci
        QueryWrapper<Location> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq(Location.LOCATION_NUMBER,idParam.getId());
        Location location = locationServiceBiz.getOne(locationQueryWrapper);
        if (location==null){
            subListAndStorageFidVo.setStorageBoxFid("");
            subListAndStorageFidVo.setSubstrateCodes(new String[0]);
            return successful(subListAndStorageFidVo);
        }
        Long locationId = location.getId();
        QueryWrapper<StorageBoxCircInfo> tsbciWrapper = new QueryWrapper<>();
        tsbciWrapper.eq(StorageBoxCircInfo.CURR_LOCATION_ID,locationId);
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getOne(tsbciWrapper);
        if (storageBoxCircInfo==null){
            subListAndStorageFidVo.setStorageBoxFid("");
            subListAndStorageFidVo.setSubstrateCodes(new String[0]);
            return successful(subListAndStorageFidVo);
        }
        //根据tsbci得到t_storage_box
        StorageBox storageBox = storageBoxServiceBiz.getById(storageBoxCircInfo.getStorageBoxId());
        if (storageBox==null||storageBox.getStorageBoxFid()==null){
            storageBoxFid = "";
        }else {
            storageBoxFid= storageBox.getStorageBoxFid();
        }
        subListAndStorageFidVo.setStorageBoxFid(storageBoxFid);
        //根据tsbci获取唯一标识
        Long locationStorageSubId = storageBoxCircInfo.getLocationStorageSubId();
        QueryWrapper<LocationStorageSub> subQueryWrapper= new QueryWrapper<>();
        subQueryWrapper.eq(LocationStorageSub.LOCATION_STORAGE_SUB_ID,locationStorageSubId)
                .orderByAsc(LocationStorageSub.SUB_POSITION);
        List<LocationStorageSub> locationStorageSubList = locationStorageSubServiceBiz.list(subQueryWrapper);
        int index = 0;
        substrateCodes = new String[locationStorageSubList.size()];
        for (LocationStorageSub locationStorageSub:locationStorageSubList){
            substrateCodes[index] = locationStorageSub.getSubCode();
            index++;
        }
        subListAndStorageFidVo.setSubstrateCodes(substrateCodes);
        return successful(subListAndStorageFidVo);
    }

    @Override
    public String getStorageFidByLocationNum(IdParam idParam){
        String storageBoxFid;
        //先获取得到tsbci
        QueryWrapper<Location> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq(Location.LOCATION_NUMBER,idParam.getId());
        Location location = locationServiceBiz.getOne(locationQueryWrapper);
        if (location==null){
            return "";
        }
        Long locationId = location.getId();
        QueryWrapper<StorageBoxCircInfo> tsbciWrapper = new QueryWrapper<>();
        tsbciWrapper.eq(StorageBoxCircInfo.CURR_LOCATION_ID,locationId);
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getOne(tsbciWrapper);
        if (storageBoxCircInfo==null){
            return "";
        }
        //根据tsbci得到t_storage_box
        StorageBox storageBox = storageBoxServiceBiz.getById(storageBoxCircInfo.getStorageBoxId());
        if (storageBox==null||storageBox.getStorageBoxFid()==null){
            storageBoxFid = "";
        }else {
            storageBoxFid= storageBox.getStorageBoxFid();
        }
        return storageBoxFid;
    }
}